package com.ccg.plat.presentation.ui.feature9.view

import android.os.Bundle
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.ccg.plat.data.local.cache.UserInfoCache
import com.ccg.plat.presentation.ui.feature9.util.JpushUtil
import com.ccg.plat.presentation.ui.feature9.viewmodel.AdminViewModel
import com.ccg.plat.ui.theme.AndroidVideoPlayerClientTheme
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

/**
 * @author : C4_雍和
 * 描述 :
 * 主要功能 :
 * 维护人员 : C4_雍和
 * date : 2025/9/19 10:12
 */
class AdminActivity : ComponentActivity() {
    private val context = this
    private val viewModel by lazy { ViewModelProvider(this)[AdminViewModel::class.java] }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        lifecycleScope.launch(Dispatchers.IO) {
            viewModel.newsChannel.send(AdminViewModel.ContentIntent.GetAgentInfo)
        }
        enableEdgeToEdge()
        setContent {
            AndroidVideoPlayerClientTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    var input by remember { mutableStateOf("") }
                    Column(
                        modifier = Modifier
                            .fillMaxSize()
                            .padding(innerPadding)
                            .padding(horizontal = 12.dp)
                    ) {
                        Row(
                            modifier = Modifier
                                .fillMaxWidth()
                                .height(55.dp), verticalAlignment = Alignment.CenterVertically
                        ) {
                            Text(text = "输入用户名:")
                            TextField(value = input, onValueChange = {
                                input = it.replace("\\s*".toRegex(), "")
                            })
                        }
                        Spacer(modifier = Modifier.height(20.dp))
                        Button(
                            onClick = {
                                if (UserInfoCache.isVip()) {
                                    if (input.trim().isNotEmpty()) {
                                        viewModel.uiState.registrationId = input.trim()
                                        MainScope().launch(Dispatchers.IO) {
                                            JpushUtil.testSendPush(errMsg = viewModel.uiState.errMsg, registrationId = input.trim(), alert = "您已开通VIP", title = "充值成功")
                                        }
                                    } else {
                                        Toast.makeText(context, "用户名不能为空", Toast.LENGTH_SHORT).show()
                                    }
                                } else {
                                    Toast.makeText(context, "你不是代理商无法操作", Toast.LENGTH_SHORT).show()
                                }

                            }, modifier = Modifier
                                .fillMaxWidth()
                                .height(55.dp)
                        ) {
                            Text(text = "开通VIP")
                        }
                        Spacer(modifier = Modifier.height(20.dp))
                        Button(
                            onClick = {
                                if (UserInfoCache.isVip()) {
                                    if (input.trim().isNotEmpty()) {
                                        viewModel.uiState.registrationId = input.trim()
                                        MainScope().launch(Dispatchers.IO) {
                                            JpushUtil.testSendPush(errMsg = viewModel.uiState.errMsg, registrationId = input.trim(), alert = "您已关闭VIP", title = "关闭VIP")
                                        }
                                    } else {
                                        Toast.makeText(context, "用户名不能为空", Toast.LENGTH_SHORT).show()
                                    }
                                } else {
                                    Toast.makeText(context, "你不是代理商无法操作", Toast.LENGTH_SHORT).show()
                                }

                            }, modifier = Modifier
                                .fillMaxWidth()
                                .height(55.dp)
                        ) {
                            Text(text = "关闭VIP")
                        }
                        Spacer(modifier = Modifier.height(20.dp))
                    }
                }
            }
        }
        viewModel.uiState.errMsg.observe(context) {
            it?.run {
                Toast.makeText(context, this, Toast.LENGTH_SHORT).show()
            }
        }
    }
}